package youleche.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;

import youleche.com.connection.JdbcHelper;
import youleche.com.idao.IHistoryInfo;
import youleche.com.model.MHistoryInfo;

/**
 * 版本历程数据访问实例
 * @author Administrator
 *
 */
public class DHistoryInfo implements IHistoryInfo {

	/**
	 * 添加版本历程信息		
	 * @param con 数据库连接
	 * @param historyInfo 版本历程
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	@Override
	public int addHistoryInfo(Connection con, MHistoryInfo historyInfo)
			throws Exception {
		PreparedStatement pre = null;
		try{
			String sql = "INSERT historyInfo(id,addTime,version,versionDesc,addPerson,A1,A2,A3,A4,A5) VALUES(?,?,?,?,?,?,?,?,?,?);";
			pre = con.prepareStatement(sql);
			pre.setObject(1, historyInfo.getId());
			pre.setObject(2, historyInfo.getAddTime());
			pre.setObject(3, historyInfo.getVersion());
			pre.setObject(4, historyInfo.getVersionDesc());
			pre.setObject(5, historyInfo.getAddPerson());
			pre.setObject(6, historyInfo.getA1());
			pre.setObject(7, historyInfo.getA2());
			pre.setObject(8, historyInfo.getA3());
			pre.setObject(9, historyInfo.getA4());
			pre.setObject(10, historyInfo.getA5());
	
			return pre.executeUpdate();
		}finally{
			JdbcHelper.free(null, pre);
		}
	}

	/**
	 * 得到版本历程的集合
	 * @param con数据库连接
	 * @return 返回历程集合
	 * @throws Exception异常
	 */
	@Override
	public List<MHistoryInfo> getHistoryList(Connection con, String startIndex,
			String endIndex) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<MHistoryInfo> list = null;
		MHistoryInfo info = null;
		try{
			String sql = "SELECT id,addTime,version,versionDesc,addPerson,A1,A2,A3,A4,A5 FROM historyInfo order by addTime desc limit "+startIndex+","+endIndex+";";
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs != null){
				list = new LinkedList<MHistoryInfo>();
				while(rs.next()){
					info = new MHistoryInfo();
					info.setId(rs.getString(1));
					info.setAddTime(rs.getString(2));
					info.setVersion(rs.getString(3));
					info.setVersionDesc(rs.getString(4));
					info.setAddPerson(rs.getString(5));
					info.setA1(rs.getString(6));
					info.setA2(rs.getString(7));
					info.setA3(rs.getString(8));
					info.setA4(rs.getString(9));
					info.setA5(rs.getString(10));
					
					list.add(info);
				}
			}
			
			return list;
			
		}finally{
			JdbcHelper.free(rs, ps);
		}
	}

	/**
	 * 得到总条数
	 * @param con 数据库连接
	 * @return 返回总条数
	 * @throws Exception
	 */
	@Override
	public String getHistoryListCount(Connection con) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			String sql = "select count(id) from historyInfo;";
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs != null){
				while(rs.next()){
					return rs.getString(1);
				}
			}
			
			return "0";
		}finally{
			JdbcHelper.free(rs, ps);
		}
	}

	/**
	 * 根据ID删除版本历程
	 * @param con 数据库连接
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	@Override
	public int deleteHistoryInfoById(Connection con, String id) throws Exception {
		PreparedStatement ps = null;
		
		try{
			String sql = "delete from historyinfo where id='"+id+"'";
			ps = con.prepareStatement(sql);
			
			return  ps.executeUpdate();
		}finally{
			JdbcHelper.free(null, ps);
		}
	}

}
